using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using AutoGear.BLL;
using ECommerce.Library.DataStructure;
using ECommerce.Library;
using System.Collections.Generic;
using Ext.Net;
using Entity;

public partial class admin_state_tax_list : SiteAdminBasePage
{
    private bool _useSearch
    {
        get
        {
            if (Session["_useSearch"] == null)
                return false;
            else return Convert.ToBoolean(Session["_useSearch"]);
        }
        set
        {
            Session["_useSearch"] = value;
        }
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!X.IsAjaxRequest)
        {
            Session["_useSearch"] = null;
        }
    }

    protected void Store1_RefreshData(object sender, StoreRefreshDataEventArgs e)
    {
        BindShippingCosts(e.Sort + (e.Dir.ToString() == "Default" ? "" : " " + e.Dir), e.Start, e.Limit);
    }

    private void BindShippingCosts(string sort, int start, int limit)
    {
        ReadWriteCustomCollection<StateTax> data = null;
        int totalCount = 0;
        start = start * limit;

        if (_useSearch && !String.IsNullOrEmpty(txtStateName.Text))
        {
            data = StateTax.GetBackendStateTaxListByStateName(txtStateName.Text, false, sort, start, limit);
            totalCount = StateTax.GetBackendStateTaxListByStateNameCount(txtStateName.Text, false);
        }
        else
        {
            data = StateTax.GetBackendStateTaxList(false, sort, start, limit);
            totalCount = StateTax.GetBackendStateTaxListCount(false);
        }

        Store1.DataSource = data;
        Store1.DataBind();

        (Store1.Proxy[0] as PageProxy).Total = totalCount;
    }

    [DirectMethod(Namespace = "Polana.WebService")]
    public void UpdateStateTax(int id, double tax, bool _isActive)
    {
        StateTax sc = StateTax.Load(id);
        sc.Tax = tax;
        sc.IsActive = _isActive;
        sc.Update();
    }

    [DirectMethod(Namespace = "Polana.WebService")]
    public void DeleteTaxes(string ids)
    {
        ids = ids.TrimStart(' ');
        string[] idArray = ids.Split(' ');
        List<int> idList = new List<int>();
        foreach (string id in idArray) idList.Add(Convert.ToInt32(id));
        StateTax.DeleteStateTax(idList);
        Store1.DataBind();
    }

    [DirectMethod(Namespace = "Polana.WebService")]
    public void SearchTax()
    {
        _useSearch = true;
        Store1.DataBind();
    }
}
